package com.itheima.edu.info.manajer.controller;

import com.itheima.edu.info.manajer.domain.Teacher;
import com.itheima.edu.info.manajer.service.TeacherService;

import java.util.Scanner;

public class TeacherController {
    private Scanner sc = new Scanner(System.in);
    private TeacherService teacherService = new TeacherService();

    public void start() {

        Tlo:
        while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
//                    System.out.println("添加老师");
                    addTeacher();
                    break;
                case "2":
//                    System.out.println("删除老师");
                    deleteTeacherById();
                    break;
                case "3":
//                    System.out.println("修改老师");
                    updateTeacher();
                    break;
                case "4":
//                    System.out.println("查看老师");
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("感谢使用老师管理系统");
                    break Tlo;
                default:
                    System.out.println("您的输入有误,请重新输入");
                    break;
            }

        }
    }

    public void updateTeacher() {
        String id = inputTeacherId();

        Teacher newTeacher = inputTeacherINnfo(id);
        //调用业务员的修改方法
        teacherService.updateTeacher(id, newTeacher);
        System.out.println("修改成功");
    }

    public void deleteTeacherById() {
        String id = inputTeacherId();

//2调用业务员中的删除方法,根据ID,删除老师
        teacherService.deleteTeacherById(id);
    }

    public void findAllTeacher() {
        //1从业务员中,获取老师对象数组
        Teacher[] teachers = teacherService.findAllTeacher();
        //2判断数组中是否有元素
        if (teachers == null) {
            System.out.println("查无信息,请添加后重试");
            return;
        }
        //3遍历数组,取出元素,并打印在控制台
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < teachers.length; i++) {
            Teacher t = teachers[i];
            if (t != null) {
                System.out.println(t.getId() + "\t" + t.getName() + "\t" + t.getAge() + "\t\t" + t.getBirthday());
            }
        }
    }

    public void addTeacher() {
        String id;
        while (true) {
            //1接收不存在的老师ID
            System.out.println("请输入老师的ID:");
            id = sc.next();
            //2判断ID是否存在
            boolean exists = teacherService.addTeacher(id);
            if (exists) {
                System.out.println("ID被占用,请重新输入");
            } else {
                break;
            }
        }
        Teacher t = inputTeacherINnfo(id);

        //5封装好的老师对象,传递给TeacherService继续完成操作
        boolean result = teacherService.addTeacher(t);

        if (result) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }


    }

    //录入老师ID
    public String inputTeacherId() {
        String id;
        while (true) {  //1键盘接收要删除的老师ID
            System.out.println("请输入ID:");
            id = sc.next();
            boolean exists = teacherService.isExists(id);
            if (!exists) {
                System.out.println("您输入的ID不存在,请重新输入:");
            } else {
                break;
            }
        }
        return id;
    }

    //    录入老师信息,封装为老师对象
    public Teacher inputTeacherINnfo(String id) {
        //3接收老师的其他信息
        System.out.println("请输入老师的姓名");
        String name = sc.next();
        System.out.println("请输入老师的年龄");
        String age = sc.next();
        System.out.println("请输入老师的生日");
        String birthday = sc.next();

        //4封装老师对象
        Teacher t = new Teacher();
        t.setId(id);
        t.setName(name);
        t.setAge(age);
        t.setBirthday(birthday);
        return t;
    }
}
